#define _CRT_SECURE_NO_WARNINGS 1

#include"Stack.h"

void StackInit(ST* p)
{
	assert(p);
	p->a = (ST*)malloc(4 * sizeof(StackDataType));
	p->capacity = 4;
	p->top = -1;
}

void StackDestory(ST* p)
{
	assert(p);
	free(p->a);
	p->a = NULL;
	p->capacity = 0;
	p->top = -1;
}

void StackPush(ST* p, StackDataType x)
{
	assert(p);
	if (p->capacity == p->top)
	{
		StackDataType* temp = realloc(p->a, 2 * p->capacity * sizeof(StackDataType));
		if (temp == NULL)
		{
			printf("realloc failed!\n");
			exit(-1);
		}
		p->a = temp;
	}
	else
	{
		p->top++;
		p->a[p->top] = x;
	}
}

void StackPop(ST* p)
{
	assert(p);
	assert(p->top > -1);
	p->top--;
}

StackDataType StackTop(ST *p)
{
	assert(p);
	assert(p->top >= 0);
	return p->a[p->top];
}

int StackSize(ST* p)
{
	assert(p);
	return ++p->top;
}

bool StackEmpty(ST* p)
{
	assert(p);
	if (p->top == -1)
	{
		return true;
	}
	return false;
}